﻿using Microsoft.AspNetCore.Html;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;


namespace Microsoft.AspNetCore.Mvc.Rendering
{
	public static class HtmlHelperExtensions
	{
		#region 下拉列表相关
		public static IHtmlContent DropDownList(this IHtmlHelper html, string expression, List<SelectListItem> selectList, string vueModelName, string cssClass, string onChange, string optionLabel, string optionValue = "")
		{
			if (!string.IsNullOrEmpty(optionLabel))
			{
				selectList.Insert(0, new SelectListItem(optionLabel, optionValue));
			}
			if (string.IsNullOrEmpty(vueModelName) && string.IsNullOrEmpty(cssClass) && string.IsNullOrEmpty(onChange))
			{
				return html.DropDownList(expression, selectList);
			}
			else
			{
				var htmlAttributes = new Dictionary<string, object>();
				if (!string.IsNullOrEmpty(vueModelName))
				{
					htmlAttributes.Add("v-model", vueModelName);
				}
				if (!string.IsNullOrEmpty(cssClass))
				{
					htmlAttributes.Add("class", cssClass);
				}
				if (!string.IsNullOrEmpty(onChange))
				{
					htmlAttributes.Add("@change", onChange);
				}
				return html.DropDownList(expression, selectList, htmlAttributes);
			}
		}

		public static IHtmlContent DropDownList(this IHtmlHelper html, string expression, List<SelectListItem> selectList, string vueModelName, string cssClass, string optionLabel, string optionValue)
		{
			if (!string.IsNullOrEmpty(optionLabel))
			{
				selectList.Insert(0, new SelectListItem(optionLabel, optionValue));
			}
			if (!string.IsNullOrEmpty(vueModelName) && !string.IsNullOrEmpty(cssClass))
			{
				return html.DropDownList(expression, selectList, new { v_model = vueModelName, @class = cssClass });
			}
			else if (!string.IsNullOrEmpty(vueModelName))
			{
				return html.DropDownList(expression, selectList, new { v_model = vueModelName });
			}
			else if (!string.IsNullOrEmpty(cssClass))
			{
				return html.DropDownList(expression, selectList, new { @class = cssClass });
			}
			else
			{
				return html.DropDownList(expression, selectList);
			}
		}
		#endregion

	}
}


